package com.imc.platform.base.menu.service.impl;

import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.menu.dao.mapper.IMenuMapper;
import com.imc.platform.base.menu.pojo.Menu;
import com.imc.platform.base.menu.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by tanweiping on 2018/8/24.
 */
@Transactional
@Slf4j
@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    IMenuMapper menuMapper;


    @Autowired
    MysqlUtil mysqlUtil;

    @Cacheable(value = "menu",key = "#menuUrl")
    public List<Menu> getMenuByMenuUrl(String menuUrl){
        List<Menu> menuList = new ArrayList<>();
        Menu menu = menuMapper.getMenuByMenuUrl(menuUrl);
        if (menu!=null){
            while (menu!=null){
                 menuList.add(menu);
                 menu = menu.getPMenu();
            }
        }
        if (!menuList.isEmpty()) Collections.reverse(menuList);
        return menuList;
    }

    @Override
    public List<Menu> queryMenuAll() {
        return menuMapper.queryMenuAll();
    }

    @Override
    public List<String> queryRoleMenuByRoleId(String roleId) {
        return menuMapper.queryRoleMenuByRoleId(roleId);
    }

    @Override
    public int addMenu(Map<String, Object> parms) {
        //String menuId=mysqlUtil.getMaxId("t_menu");
        //parms.put("id",menuId);
        Menu menu=new Menu();
        //parms.put("sn", String.valueOf(Integer.parseInt(menuMapper.queryMenuSnByParentId(parms.get("parentId").toString()))) );
        if("0".equals(parms.get("parentId"))){
            parms.put("level","1");
        }else {
            menu=menuMapper.queryMenuByMenuId(parms.get("parentId").toString());
            if(null !=menu && !"".equals(menu)){
                parms.put("level",menu.getLevel()+1);
            }
        }
        return menuMapper.addMenu(parms);
    }

    @Override
    public int addMenuFirstLevel() {
//        MysqlUtil mysqlUtil=new MysqlUtil();
        String menuId=mysqlUtil.getMaxId("t_menu");
        Map<String,Object>parm=new HashMap<>();
        parm.put("id",menuId);
        parm.put("level","1");
        parm.put("parentId","0");
        parm.put("menuName","新菜单");
        parm.put("menuType","1");
        return menuMapper.addMenu(parm);
    }

    @Override
    public int updateMenuByMenuId(Map<String,Object> parmsMap) {
        return menuMapper.updateMenuByMenuId(parmsMap);
    }

    @Override
    public int delMenuById(String id,String parentId) {
        List<String>retList=menuMapper.queryMenuIdBylastId(id);
        return menuMapper.delMenuById(retList);
    }

    @Override
    public int queryIsNameRep(String menuName) {
        int count= menuMapper.queryIsNameRep(menuName);
        return count;
    }

    @Override
    public Menu queryMenuByMenuId(String menuId) {
        return menuMapper.queryMenuByMenuId(menuId);
    }
}
